﻿//[assembly: WebActivator.PreApplicationStartMethod(typeof(Deixei.Web.DeixeiHttpApplication), "Start")]
//[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(Deixei.Web.DeixeiHttpApplication), "Stop")]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Deixei;
using Deixei.Common;
using Deixei.Domain;
using Deixei.Domain.Repository;
using Deixei.Domain.Service;
using Deixei.Infrastructure;
using Deixei.Web.Controllers;
using Deixei.Web.Filters;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;


namespace Deixei.Web
{
    public class DeixeiHttpApplication : HttpApplication
    {
        public Assembly CurrentAssembly = Assembly.GetExecutingAssembly();
        public static IUnityContainer Container;

        public static void Initialise()
        {
            Container = new UnityContainer();
            // register all your components with the container here         
            RegisterComponents();
            // e.g. container.RegisterType<itestservice, testservice="">();
            //DependencyResolver.SetResolver(new UnityDependencyResolver(Container));

            // register dependency resolver for WebAPI
            //GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(Container);
        }

        public void Configure()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
         
            System.Data.Entity.Database.SetInitializer<DataContext>(new DataContextInitializer());

            //using (DataContext TestContext = new DataContext())
            //{
            //    if (TestContext.Users.Count() == 0)
            //    {
            //        throw new Exception("No administrator defined");
            //    }
            //}


            //GlobalConfiguration.Configuration.DependencyResolver = new IoCContainer(UnityContainer);
            //GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi
        }

        private static void RegisterComponents()
        {

            Container.AddNewExtension<Interception>();
            Container.RegisterInstance<DataContext>(new DataContext());
            Container.RegisterType<IValuesService, ValuesService>();
            Container.RegisterType<IUnitOfWork, UnitOfWork>();
            

            Container.RegisterType<DeixeiViewMasterPage>();
            Container.RegisterType<DeixeiViewPage>();
            Container.RegisterType<DeixeiViewUserControl>();


            Container.RegisterInstance<IValuesService>(new ValuesService());

            Container.RegisterType<IViewPageActivator, DeixeiViewPageActivator>(new InjectionConstructor(Container));

            Container.RegisterInstance<IFilterProvider>("FilterProvider", new FilterProvider(Container));
            Container.RegisterInstance<IActionFilter>("LogActionFilter", new TraceActionFilter());

            //Container.RegisterInstance<ActionLogRepository>(new ActionLogRepository(Container.Resolve<UnitOfWork>()));


            IDependencyResolver resolver = DependencyResolver.Current;
            IDependencyResolver newResolver = new UnityDependencyResolver(Container, resolver);
            DependencyResolver.SetResolver(newResolver);


            //Unity.RegisterTypeHelper<IRepository<Ingredient>, Repository<Ingredient>>();

            //Unity.RegisterTypeHelper<IController, DeixeiController>();

        }

    }
}
